package io.github.uicdb.flame_driven.client.block;

import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.math.Vector3f;
import io.github.uicdb.flame_driven.common.block.BELifeAggregator;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.block.model.ItemTransforms;
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraftforge.items.CapabilityItemHandler;
import org.jetbrains.annotations.NotNull;

public class RendererLifeAggregatorLike implements BlockEntityRenderer<BELifeAggregator> {
    @Override
    public void render(@NotNull BELifeAggregator pBlockEntity, float pPartialTick, @NotNull PoseStack pPoseStack, @NotNull MultiBufferSource pBufferSource, int pPackedLight, int pPackedOverlay) {
        pBlockEntity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).ifPresent(iItemHandler -> {
            var itemStack=iItemHandler.getStackInSlot(1).isEmpty()?iItemHandler.getStackInSlot(0):iItemHandler.getStackInSlot(1);
            var itemRenderer= Minecraft.getInstance().getItemRenderer();
            var bakedModel=itemRenderer.getModel(itemStack,pBlockEntity.getLevel(),null,0);
            var itemRenderHeight=1.5d;
            pPoseStack.pushPose();
            pPoseStack.translate(0.5d,itemRenderHeight,0.5d);
            pPoseStack.scale(0.5f,0.5f,0.5f);
            pPoseStack.mulPose(Vector3f.YN.rotationDegrees(pBlockEntity.getRenderRotate()));
            itemRenderer.render(itemStack, ItemTransforms.TransformType.FIXED,false,pPoseStack,pBufferSource,0xF00_0F0, OverlayTexture.NO_OVERLAY,bakedModel);
            pPoseStack.popPose();
        });

    }

    @Override
    public boolean shouldRenderOffScreen(@NotNull BELifeAggregator pBlockEntity) {
        return true;
    }
}
